package cn.iocoder.yudao.module.erp.controller.app.auth;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.config.SecurityProperties;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.erp.controller.app.auth.vo.*;
import cn.iocoder.yudao.module.erp.service.AppCaptchaService;
import cn.iocoder.yudao.module.erp.service.auth.ErpAuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "ERP供应商登录接口")
@RestController
@RequestMapping("/erp/supplier/auth")
@Validated
@Slf4j
public class AppErpAuthController {


    @Resource
    private SecurityProperties securityProperties;

    @Resource
    private ErpAuthService authService;

    @Autowired
    private AppCaptchaService captchaService;


    @PostMapping("/login")
    @Operation(summary = "使用企业全称 + 密码登录")
    @PermitAll
    public CommonResult<AppAuthLoginRespVO> login(@RequestBody @Valid AppAuthLoginReqVO reqVO) {
        return success(authService.login(reqVO));
    }

    @PostMapping("/logout")
    @Operation(summary = "登出系统")
    @PermitAll
    public CommonResult<Boolean> logout(HttpServletRequest request) {
        String token = SecurityFrameworkUtils.obtainAuthorization(request,
                securityProperties.getTokenHeader(), securityProperties.getTokenParameter());
        if (StrUtil.isNotBlank(token)) {
            authService.logout(token);
        }
        return success(true);
    }

    @PostMapping("/refresh-token")
    @Operation(summary = "刷新令牌")
    @Parameter(name = "refreshToken", description = "刷新令牌", required = true)
    @PermitAll
    public CommonResult<AppAuthLoginRespVO> refreshToken(@RequestParam("refreshToken") String refreshToken) {
        return success(authService.refreshToken(refreshToken));
    }


    // ========== 短信登录相关 ==========

    @GetMapping("/getCaptchaImage")
    @Operation(summary = "获取图形验证码")
    @PermitAll
    public CommonResult<CaptchaRespVO> getCode(HttpServletResponse response) throws IOException
    {
        return success(captchaService.getCaptcha());
    }

    @PostMapping("/sendSmsCodeRegister")
    @Operation(summary = "注册发送手机短信验证码")
    @PermitAll
    public CommonResult<Boolean> sendSmsCodeRegister(@RequestBody @Valid AppAuthSmsSendReqVO reqVO) {
        authService.sendSmsCodeRegister(reqVO);
        return success(true);
    }


    @PostMapping("/sendSmsCodeLogin")
    @Operation(summary = "登录发送手机短信验证码")
    @PermitAll
    public CommonResult<Boolean> sendSmsCodeLogin(@RequestBody @Valid AppAuthSmsSendReqVO reqVO) {
        authService.sendSmsCodeLogin(reqVO);
        return success(true);
    }

    @PostMapping("/smsLogin")
    @Operation(summary = "使用手机 + 验证码登录")
    @PermitAll
    public CommonResult<AppAuthLoginRespVO> smsLogin(@RequestBody @Valid AppAuthSmsLoginReqVO reqVO) {
        return success(authService.smsLogin(reqVO));
    }



    @PostMapping("/sendSmsCodePasswordReset")
    @Operation(summary = "找回密码发送手机短信验证码")
    @PermitAll
    public CommonResult<Boolean> sendSmsCodePasswordReset(@RequestBody @Valid AppAuthSmsSendReqVO reqVO) {
        authService.sendSmsCodePasswordReset(reqVO);
        return success(true);
    }

    @PostMapping("/resetPassword")
    @Operation(summary = "找回密码 - 使用手机 + 短信验证码")
    @PermitAll
    public CommonResult<Boolean> resetPassword(@RequestBody @Valid AppResertPasswordReqVO reqVO) {
        authService.resetPassword(reqVO);
        return success(true);
    }





}
